<?php

/**
 * This is the model class for table "personality_questions".
 *
 * The followings are the available columns in table 'personality_questions':
 * @property string $id
 * @property string $text
 * @property string $sex
 * @property integer $active
 * @property integer $order
 */
class mPersonalityQuestions extends CActiveRecord {

  /**
   * Returns the static model of the specified AR class.
   * @param string $className active record class name.
   * @return mPersonalityQuestions the static model class
   */
  public static function model($className = __CLASS__) {
    return parent::model($className);
  }

  /**
   * @return string the associated database table name
   */
  public function tableName() {
    return 'personality_questions';
  }

  /**
   * @return array validation rules for model attributes.
   */
  public function rules() {
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array('id, text, sex, order', 'required'),
        array('active, order', 'numerical', 'integerOnly' => true),
        array('id', 'length', 'max' => 512),
        array('sex', 'length', 'max' => 6),
        // The following rule is used by search().
        // Please remove those attributes that should not be searched.
        array('id, text, sex, active, order', 'safe', 'on' => 'search'),
    );
  }

  /**
   * @return array relational rules.
   */
  public function relations() {
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
    );
  }

  /**
   * @return array customized attribute labels (name=>label)
   */
  public function attributeLabels() {
    return array(
        'id' => 'ID',
        'text' => 'Text',
        'sex' => 'Sex',
        'active' => 'Active',
        'order' => 'Order',
    );
  }

  /**
   * Retrieves a list of models based on the current search/filter conditions.
   * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
   */
  public function search() {
    // Warning: Please modify the following code to remove attributes that
    // should not be searched.

    $criteria = new CDbCriteria;

    $criteria->compare('id', $this->id, true);
    $criteria->compare('text', $this->text, true);
    $criteria->compare('sex', $this->sex, true);
    $criteria->compare('active', $this->active);
    $criteria->compare('order', $this->order);

    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
  }

  public function getDataPerQuesWithUserAns($sex, $use_id) {
    $rows = db()->createCommand()
            ->select('pq.*, ua.text as text1')
            ->from('personality_questions pq')
            ->leftJoin('user_answer_question ua', 'ua.question_id=pq.id and ua.user_id=:user_id',array(':user_id'=>$use_id))
            ->where('sex=:sex', array(':sex' => $sex))
            ->order('ordinal asc')
            ->queryAll();
    if (empty($rows)) {
      return false;
    }
    return $rows;
  }

  public function getQuesUserAns($sex, $use_id) {
    $rows = db()->createCommand()
            ->select('pq.*, ua.text as text1')
            ->from('personality_questions pq')
            ->leftJoin('user_answer_question ua', 'ua.question_id=pq.id and ua.user_id=:user_id',array(':user_id'=>$use_id))
            ->where('sex=:sex and ua.text is not NULL', array(':sex' => $sex))
            ->order('ordinal asc')
            ->queryAll();
    if (empty($rows)) {
      return false;
    }
    return $rows;
  }
}